package io.finer.erp.jeecg.finance.service.impl;

import io.finer.erp.jeecg.finance.entity.FinaFurl;
import io.finer.erp.jeecg.finance.entity.FinaFurlEntry;
import io.finer.erp.jeecg.finance.mapper.FinaFreightMapper;
import io.finer.erp.jeecg.finance.mapper.FinaFurlEntryMapper;
import io.finer.erp.jeecg.finance.mapper.FinaFurlMapper;
import io.finer.erp.jeecg.finance.service.IFinaFurlService;
import io.finer.erp.jeecg.stock.vo.FreightOrIoBillVo;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 收货单
 * @Author: jeecg-boot
 * @Date:   2021-06-15
 * @Version: V1.0
 */
@Service
public class FinaFurlServiceImpl extends ServiceImpl<FinaFurlMapper, FinaFurl> implements IFinaFurlService {

	@Autowired
	private FinaFurlMapper finaFurlMapper;
	@Autowired
	private FinaFurlEntryMapper finaFurlEntryMapper;
	@Autowired
    private FinaFreightMapper finaFreightMapper;
	
	@Override
	@Transactional
	public void saveMain(FinaFurl finaFurl, List<FinaFurlEntry> finaFurlEntryList) {
		finaFurlMapper.insert(finaFurl);
		if(finaFurlEntryList!=null && finaFurlEntryList.size()>0) {
			for(FinaFurlEntry entity:finaFurlEntryList) {
				//外键设置
				entity.setMid(finaFurl.getId());
				finaFurlEntryMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FinaFurl finaFurl,List<FinaFurlEntry> finaFurlEntryList) {
		finaFurlMapper.updateById(finaFurl);
		
		//1.先删除子表数据
		finaFurlEntryMapper.deleteByMainId(finaFurl.getId());
		
		//2.子表数据重新插入
		if(finaFurlEntryList!=null && finaFurlEntryList.size()>0) {
			for(FinaFurlEntry entity:finaFurlEntryList) {
				//外键设置
				entity.setMid(finaFurl.getId());
				finaFurlEntryMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		finaFurlEntryMapper.deleteByMainId(id);
		finaFurlMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			finaFurlEntryMapper.deleteByMainId(id.toString());
			finaFurlMapper.deleteById(id);
		}
	}

	@Override
	public List<FreightOrIoBillVo> getBullByBuy() {
		// TODO Auto-generated method stub
		List<String> billNoList = finaFreightMapper.getBillNo("0");
		return baseMapper.getFurlCode(billNoList);
	}
	
}
